<?php
namespace Admin\Controller;
use Think\Controller;
use Myself\FileSystem;
use Admin\Controller\CommonController;
class RuleController extends CommonController {

	protected $statusArr=array("下架","上架");
	protected $isHome=array("未推荐","已推荐");

	
    public function index(){
		header('Content-Type:text/html;charset=utf-8');
		
		//-----------------搜索-------------------
	
		$sh_q = I('sh_q','');//关键字
		if(!empty($sh_q)){
			$shArray['sh_q'] = $sh_q;
			$map['id'] = $sh_q;
			$map['title'] = array('LIKE','%'.$sh_q.'%');
			$map['content'] = array('LIKE','%'.$sh_q.'%');
			$map['_logic'] = 'OR';
			$where['_complex'] = $map;
		}


		$from_time = I('from_time','');
		$to_time = I('to_time','');
		  
		if($from_time){
			$from_time_s = strtotime($from_time);
			$where['addtime']= array('EGT',$from_time_s);
			$shArray['from_time'] = $from_time;
		}
		
		if($to_time){
			$to_time_s = strtotime($to_time)+86400;
			$where['addtime']= array('ELT',$to_time_s);
			$shArray['to_time'] = $to_time;	
		}		
		
		if($from_time && $to_time){
			$where['add_time']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
		}	
			  
		if(!empty($shArray)){$this->assign('shArray',$shArray);}
		//-------------------------搜索end-----------------

		$pageno = I('p',1);
                $pageCount = 10;
		$list = M('administrator')->where($where)->order('id desc')->page($pageno.','.$pageCount)->select();
		
		foreach( $list as $key => $item){
		
		$list[$key]['author_name'] = M('administrator')->where('id="'.$item['author'].'"')->getField('username');
		
		}
             
	        $this->assign('list',$list);
		$count = M('administrator')->where($where)->count();
		$this->assign('count',$count);
		$Page = new \Think\Page($count,$pageCount);
		$show = $Page->showAdmin();
		$this->assign('page',$show);
                $this->display('Rule/index');
    }
		 
  public  function edit(){
          $id = I('id');
	  $dataInfo = M('administrator')->where('id="'.$id.'"')->find($id);
	  $typeArr  = array(1=>'一级',2=>'二级');
	  if( $dataInfo ){
		 $this->assign('dataInfo', $dataInfo);
		 $this->assign('typeArr', $typeArr);
		 $this->display();
	  }

	}
	
  public  function  saveEdit(){
                 $id = I('updateid');
		 $pwd = M('administrator')->where('id="'.$id.'"')->getField('password');
		 
		 if(I('password') == $pwd ){
		   $data['username']= I('username');
                   $data['phone'] = I('phone');
                   $data['fullname'] = I('fullname');
		 }else{
		   $data['username']= I('username');
                   $data['phone'] = I('phone');
                   $data['password'] = md5(I('password'));
		   $data['fullname'] = I('fullname');
		 }
	       if($data){
		   $result = M('administrator')->where('id="'.$id.'"')->save($data);
		 }
		    $info['status'] =100;
		    $info['info'] = '修改成功';
		    $this->ajaxReturn($info);
		
  }	
  
  
     public function add(){
		 $typeArr  = array(1=>'一级',2=>'二级');
		 $this->assign('typeArr', $typeArr);
		 $this->display();
    }
	
	
	public  function save(){  
	         $data['username']= I('username');
                 $data['phone'] = I('phone');
                 $data['password'] = md5(I('password'));
		 $data['author'] = $_SESSION['admin_user_id'];
		 $data['fullname'] = I('fullname');
		 $data['add_time'] = time();
		// $data['level'] = I('level');
		
	     if( $data ){
                  $result = M('administrator')->add($data);		 
		 }
	        $info['status'] = 100;
		 $info['info'] = '添加成功';
		 $this->ajaxReturn($info);
		 
   }
	
	 public function del(){
            $id = $_REQUEST['id'];
		   if (is_array($id)){
               $where = 'id in('.implode(',', $id).')';
            } else {
               $where = 'id='.$id;
            }
	   $list = M('administrator')->where($where)->delete();
	   if( $list){
		 $this->redirect('Rule/index');
	    }
         }

	
	
	
	 
}